<template>
    <LDrawerForm
        :visible="visible"
        :confirmLoading="confirmLoading" 
        :title="title" 
        ref="drawerForm" 
        :formAttributes="formAttributes" 
         :width="width"
        @handleOk="handleOk" 
        @handleCancel="handleCancel"
        @changeEventHandler="changeEventHandler">
    </LDrawerForm>

</template>

<script>
import LDrawerForm from '@/components/lt/form/LDrawerForm'
import pick from 'lodash.pick'
import request from '@/utils/request'
import defaultSettings from '@/config/defaultSettings'


export default {
    name:'noticeForm',
    components:{
        LDrawerForm
    },
    data(){
        return{
            title:'详情',
            visible:false,
            confirmLoading: false,
            width:document.documentElement.clientWidth-200,
            model:{},
            formAttributes:{
              type:'',//collapse,tabs
              formItems:[
                {
                  title:'标签一',
                  columns:1,
                  fields:[
                    {field:'noticeTitle',label: '标题',component: 'input',rules: [{required: true, message: '不能为空'}]},
                    {field:'noticeContent',label:'内容',component:'tinymce', height:500,width:896,rules: [{required: true, message: '不能为空'}]},
                     {field:'isUse',label:'是否可用',component:'select',dictCode:defaultSettings.数据字典.是否,initialValue:'1',rules: [{required: true, message: '不能为空'}]},
                     {field:'noticeResource',label:'资源',component:'fileUp'}
                  ]
                },
              ]

            }


        }
    },
    methods:{
        edit (record) {
            this.visible = true;
            this.model = Object.assign({}, record);
            //将this.model 传入编辑器
              this.formAttributes.formItems[0].fields[1]["model"]=this.model
              //将内容传入编辑器
             this.formAttributes.formItems[0].fields[1]["content"] = this.model["noticeContent"]
            this.$nextTick(() => {
                this.$refs.drawerForm.getForm().setFieldsValue(pick(this.model,'noticeTitle','isUse','noticeContent','noticeResource'))
            })
        },
        handleOk(){
            let that = this
            if(!this.model["noticeContent"]){
                that.$message.warning("内容为空")
                return false
            }
            this.$refs.drawerForm.getForm().validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true
                    let formData = Object.assign(this.model, values);
                    request({url: '/KhyNoticeController/save',method: 'post',data: formData}).then((res)=>{
                        if(res.success){
                           that.close();
                            that.$message.success(res.message);
                            that.edit(res.result)
                        }else{
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                         that.visible = false;
                    })
                    
                }
            })
        },
        handleCancel(){
            this.visible=false
        },
        close () {
            this.$emit('close');
            this.visible = false;
        },
        /**
         * 表单内容发生变化
         */
        changeEventHandler(changeItem){
            console.log(changeItem)
           
        }
    },
    created(){
        
    },
}
</script>